package com.ociney.ocineydownloader;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import java.io.File;

public class MainActivity extends Activity implements View.OnClickListener{

    String url = "https://pop-cine.googlecode.com/svn/trunk/app/build/apk/app-debug-unaligned.apk";
    String _package = "com.sixney.app";
    String apk = "ociney.apk";
    View boutonTelecharger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        charger();
        remplir();
        ajouterListeners();
    }

    private void ajouterReceiver(){

        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {

                    //INSTALL

                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/"+apk)), "application/vnd.android.package-archive");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);
                }
            }
        };

        registerReceiver(receiver, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    private void charger() {
        boutonTelecharger = findViewById(R.id.button);
    }
    private void remplir() {
    }
    private void ajouterListeners() {
        boutonTelecharger.setOnClickListener(this);
    }

    public void onClick(View v){

        //DESINSTALATION
        if(isAppInstalled(_package)) {
            Intent d = new Intent(Intent.ACTION_DELETE);
            d.setData(Uri.parse("package:" + _package));
            startActivity(d);
        }

        File file=new File(getFilesDir(), "/download/"+apk);

        //DOWNLOAD
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setTitle("Téléchargement de O'Ciney");
// in order for this if to run, you must use the android 3.2 to compile your app
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "ociney.apk");

// get download service and enqueue file
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);

        ajouterReceiver();
    }

    private boolean isAppInstalled(String uri) {
        PackageManager pm = getPackageManager();
        boolean installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            installed = false;
        }
        return installed;
    }
}
